package cn.schoolwow.util.module.query.instanceList.flow.filter;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.module.query.instanceList.domain.InstanceListQueryOption;

import java.util.List;

public class FilterInstanceByInstanceValueFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        filterByInstanceValue(flowContext);
        filterByInstanceValueList(flowContext);
    }

    @Override
    public String name() {
        return "根据实例值过滤实例列表";
    }

    private void filterByInstanceValue(FlowContext flowContext){
        InstanceListQueryOption instanceQueryOption = flowContext.checkInstanceData(InstanceListQueryOption.class);
        List<Object> filterInstanceList = (List<Object>) flowContext.getData("filterInstanceList");

        if(null==instanceQueryOption.singleValue){
            return;
        }
        filterInstanceList.removeIf(instance->{
            if(!instanceQueryOption.singleValue.equals(instance)){
                return true;
            }
            return false;
        });
    }

    private void filterByInstanceValueList(FlowContext flowContext){
        InstanceListQueryOption instanceQueryOption = flowContext.checkInstanceData(InstanceListQueryOption.class);
        List<Object> filterInstanceList = (List<Object>) flowContext.getData("filterInstanceList");

        if(null==instanceQueryOption.singleValueList){
            return;
        }
        filterInstanceList.removeIf(instance->{
            if(!instanceQueryOption.singleValueList.contains(instance)){
                return true;
            }
            return false;
        });
    }
}
